<template>
  <div class="home">
 
 
 

    <van-button type="primary" @click="pay">主要按钮</van-button>
 <van-button type="primary" @click="orderquery">订单查询</van-button>


       <RouterLink to="/cont">Go to Home</RouterLink>
  </div>
</template>
    
<script>

import wxjsapi from 'weixin-jsapi'
import { Loading } from 'vant';

import basicMinin from '@/mixins/basicMinin.js'
export default {
  name: 'home',
 mixins: [basicMinin],
  
  data() {
    return {
      
      notice: "",
      logo: "",
      basic: null,
      ident: 0,
      nowtime: "",
      openstatus: -1,

      rows: [],
    }
  },
  computed: {


  },
  components: {

  },


  created() {


  },
  mounted() {

    this.notice+="xxx"
    console.log("xxx");   
 
   


  },

  methods: {

   async  pay(){

      var res=  await  this.$rq.post("wxpay",{op:"pay"})
      if(res["status"]==1){

      var payParams=  res["package"] 

       // 调用微信支付
        if (typeof WeixinJSBridge == "undefined") {
          if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', () => this.onBridgeReady(payParams), false);
          } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', () => this.onBridgeReady(payParams));
            document.attachEvent('onWeixinJSBridgeReady', () => this.onBridgeReady(payParams));
          }
        } else {
          this.onBridgeReady(payParams);
        }

      }

      
    },

   async  orderquery(){
 var res=  await  this.$rq.post("wxpay",{op:"orderquery",out_trade_no:"202508021732309200049"})
      if(res["status"]==1){
        console.log("订单查询结果：", res);
      }else{
        console.error("订单查询失败：", res);
      }

   },
    


    handlePay() {
      // 显示加载状态
      this.$loading({
        lock: true,
        text: '准备支付...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      
      // 调用后端接口获取支付参数
      this.$axios.post('/api/getWxPayParams', {
        orderId: this.orderId
      }).then(res => {
        // 关闭加载状态
        this.$loading().close();
        
        const payParams = res.data;
        console.log('获取支付参数成功', payParams);
        
        // 调用微信支付
        if (typeof WeixinJSBridge == "undefined") {
          if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', () => this.onBridgeReady(payParams), false);
          } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', () => this.onBridgeReady(payParams));
            document.attachEvent('onWeixinJSBridgeReady', () => this.onBridgeReady(payParams));
          }
        } else {
          this.onBridgeReady(payParams);
        }
      }).catch(error => {
        this.$loading().close();
        console.error('获取支付参数失败', error);
        this.$message.error('支付初始化失败，请稍后重试');
      });
    },
    
    onBridgeReady(payParams) {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          appId: payParams.appId,
          timeStamp: payParams.timeStamp,
          nonceStr: payParams.nonceStr,
          package: payParams.package,
          signType: payParams.signType,
          paySign: payParams.paySign
        },
        (res) => {
          if (res.err_msg == "get_brand_wcpay_request:ok") {
            this.$message.success('支付成功！');
            setTimeout(() => {
              window.location.href = '/payment/success?orderId=' + this.orderId;
            }, 1500);
          } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
            this.$message.info('用户取消支付');
          } else {
            this.$message.error(`支付失败: ${res.err_msg}`);
          }
        }
      );
    },
 
    
    async getwebinfo() {



      let { status, obj } = await this.$rq.get("test/webconfig")

      let { notice, logo } = obj.basic;



      this.basic = obj.basic;
      this.notice = notice;
      this.logo = logo 

    }

  }
}
</script>
<style lang="less"> 
</style>
    
    